import React, {useEffect} from 'react';
import {Grid} from "@mui/material";
import {get163Search} from '../../utils/request/163request'
import {getQSearch} from '../../utils/request/qrequest'
import {getMgSearch} from '../../utils/request/migurequest'
import {getKwSearch} from '../../utils/request/kwrequest'
import Singer from "../Singer/Singer";
import Loading from "../Loading/Loading";


export default function SingersTable(props) {


    let {from} = props;

    const [rows, setRows] = React.useState([]);
    const [keywords, setKeywords] = React.useState(props.keywords);
    const [loading, setLoading] = React.useState(false);
    if(props.keywords !== keywords){
        setKeywords(props.keywords)
    }
    function init(){
        //console.log(from + "  from   " + keywords)
        let result;
        if(from === '163'){

            result = //默认为 1 即单曲 , 取值意义 : 1: 单曲, 10: 专辑, 100: 歌手, 1000: 歌单, 1002: 用户, 1004: MV, 1006: 歌词, 1009: 电台, 1014: 视频, 1018:综合, 2000:声音(搜索声音返回字段格式会不一样)
                get163Search(keywords,0,100,100,(r) => {
                    setRows(r)
                    setLoading(false);
                    //console.log(r)
                }).catch(() => {
                    setLoading(false)
                })
        }else if(from === 'qq'){
            result = // 0：单曲，2：歌单，7：歌词，8：专辑，9：歌手，12：mv
            getQSearch(keywords,1,20,9,(r) => {
                setRows(r)
                setLoading(false);
                //console.log(r)
            }).catch(() => {
                setLoading(false)
            })
        }
        else if(from === 'migu'){
            //支持：song, playlist, mv, singer, album, lyric
            result = getMgSearch(keywords,1,100,'singer',(r) => {
                setRows(r)
                setLoading(false);
                //console.log(r)
                    }).catch(() => {
                setLoading(false)
            })
        }
        else if(from === 'kugou'){
            //酷狗只有歌曲搜索
            let r = [{
                id:-1,
                name: '酷狗只有歌曲搜索！',
                img: undefined,
                from:'kugou'
            }]
            setRows(r)
            setLoading(false);

        }
        else if(from === 'kuwo'){
            result = //0歌曲 1 专辑 2 歌单 3 歌手
            getKwSearch(keywords,1,100,3,(r) => {
                setRows(r)
                setLoading(false);
                //console.log(r)
            }).catch(() => {
                setLoading(false)
            })
        }

    }
    useEffect(async () => {
        if(keywords === ""){
            setLoading(false);
        }else
            setLoading(true);
        await init()

    },[keywords])
    return (
        <>
            <Loading open={loading}/>
            <Grid container
                  direction="row"
                  justifyContent="left"
                  alignItems="center"
                  xs={12}
                  >
                {rows.map((r) => {
                    return <Singer name={r.name} id={r.id} img={r.img} from={r.from} allSinger={rows}/>
                })}


            </Grid>
            {/*<Stack direction="row" spacing={2}>*/}
            {/*    <Singer />*/}
            {/*    <Singer />*/}
            {/*    <Singer />*/}
            {/*    <Singer />*/}
            {/*</Stack>*/}
        </>
    )
}